package edu.whu.notepal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.whu.notepal.mapper.SubscriberMapper;
import edu.whu.notepal.pojo.Group;
import edu.whu.notepal.pojo.Subscriber;
import edu.whu.notepal.pojo.User;
import edu.whu.notepal.service.GroupService;
import edu.whu.notepal.service.NoteService;
import edu.whu.notepal.service.SubscriberService;
import edu.whu.notepal.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 陈磊
* @description 针对表【t_subscribers】的数据库操作Service实现
* @createDate 2022-11-29 21:00:21
*/
@Service
public class SubscriberServiceImpl extends ServiceImpl<SubscriberMapper, Subscriber>
    implements SubscriberService {

    @Resource
    NoteService noteService;

    @Resource
    GroupService groupService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addGroup(Long userId, Long groupId) {
        Subscriber subscriber = new Subscriber();
        subscriber.setGroupId(groupId);
        subscriber.setSubscriberId(userId);
        save(subscriber);
        Group group = groupService.getById(groupId);
        group.setActualNumbers(group.getActualNumbers()+1);
        groupService.updateById(group);
        if(group.getActualNumbers().equals(group.getExceptedNumbers())){
            noteService.changeVisible(groupId,false);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void exitGroup(Long userId, Long groupId) {
        LambdaQueryWrapper<Subscriber> subscribersWrapper = new LambdaQueryWrapper<>();
        subscribersWrapper.eq(Subscriber::getSubscriberId,userId);
        subscribersWrapper.eq(Subscriber::getGroupId,groupId);
        remove(subscribersWrapper);
        Group group = groupService.getById(groupId);
        if(group.getActualNumbers().equals(group.getExceptedNumbers())){
            noteService.changeVisible(groupId,true);
        }
        group.setActualNumbers(group.getActualNumbers()-1);
        groupService.updateById(group);
    }

    @Override
    public List<Long> getSubscribersId(Long groupId) {
        LambdaQueryWrapper<Subscriber> subscriberWrapper = new LambdaQueryWrapper<>();
        subscriberWrapper.eq(Subscriber::getGroupId,groupId);
        subscriberWrapper.select(Subscriber::getSubscriberId);
        List<Subscriber> subscribers = list(subscriberWrapper);
        List<Long> users = subscribers.stream()
                .map(Subscriber::getSubscriberId).
                collect(Collectors.toList());
        return users;
    }

    @Override
    public List<Group> getGroups(Long id) {
        LambdaQueryWrapper<Subscriber> subscriberWrapper = new LambdaQueryWrapper<>();
        subscriberWrapper.eq(Subscriber::getSubscriberId,id);
        subscriberWrapper.select(Subscriber::getGroupId);
        return
                list(subscriberWrapper).stream().map(
                subscriber->{
                    LambdaQueryWrapper<Group> groupLambdaQueryWrapper = new LambdaQueryWrapper<Group>()
                            .eq(Group::getNoteId, subscriber.getGroupId());
                    Group group = groupService.getOne(groupLambdaQueryWrapper);
                    group.setColumnId(noteService.getById(group.getNoteId()).getColumnId());
                    return group;
                }
        ).collect(Collectors.toList());
    }
}